package com.nepenthe.view;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.nepenthe.Utils.UserGuiUtils;
import com.nepenthe.pojo.Student;
import com.nepenthe.pojo.User;
import org.jb2011.lnf.beautyeye.ch3_button.BEButtonUI;

import javax.swing.*;
import java.awt.*;
import java.util.List;

public class userInfo {
    /**
     * 左侧菜单栏
     */

    public void userInfoInit(User user) {
        JFrame jFrame = UserGuiUtils.userGuiInit();
        //初始化--往窗体里放其他控件
        init(jFrame, user);
        //设置窗体可见
        jFrame.setVisible(true);
    }

    public void init(JFrame jFrame, User user) {
        //头部信息
        JPanel titlePanel = new JPanel();
        titlePanel.setLayout(new FlowLayout());
        titlePanel.add(new JLabel("个人信息"));
        jFrame.getContentPane().add(titlePanel, "North");
        //左侧信息栏
        JPanel userInfoMenu = new JPanel();
        userInfoMenu.setLayout(new GridLayout(4, 1));

        JButton userInfoBtn = new JButton("个人信息");
        userInfoBtn.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.green));
        JButton updateBtn = new JButton("更新信息");
        updateBtn.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.lightBlue));
        JButton pwdBtn = new JButton("修改密码");
        pwdBtn.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.blue));
        JButton exitBtn = new JButton("退出登录");
        exitBtn.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.red));

        Student student = getUserInfo(user);

        JPanel userInfo = new JPanel();
        userInfo.setLayout(new GridLayout(4, 2));
        userInfo.add(new Label("姓名:  " + user.getName()));
        userInfo.add(new Label("性别:  " + student.getSex()));
        userInfo.add(new Label("户籍:  " + student.getAddress()));
        userInfo.add(new Label("生肖:  " + student.getZodiac()));
        userInfo.add(new Label("生日:  " + student.getBirthday()));
        userInfo.add(new Label("星座:  " + student.getXingzuo()));
        userInfo.add(new Label("学号:  " + user.getStudentID()));
        userInfo.add(new Label("身份证号:  " + user.getIdCard()));
        jFrame.add(userInfo);

        userInfoMenu.add(userInfoBtn);
        userInfoMenu.add(updateBtn);
        userInfoMenu.add(pwdBtn);
        userInfoMenu.add(exitBtn);
        jFrame.add(userInfoMenu, "West");


    }

    public Student getUserInfo(User user) {
        String result = HttpUtil.get("http://api.tianapi.com/txapi/sfz/index?key=6e8add6960b3ba59ba61082ea0320242&idcard=" + user.getIdCard());
        System.out.println(result);
        JSONObject jsonObject = JSONUtil.parseObj(result);
        Object newslist = jsonObject.get("newslist");
        String s = JSONUtil.toJsonStr(newslist);
        List<Student> students = JSONUtil.toList(s, Student.class);
        System.out.println(students);
        Student student = students.get(0);
        System.out.println(student.toString());
        return student;
    }
}
